﻿using Cart.SanPhamSrv;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Cart
{
    public partial class ChiTiet :VdcInc.vdcAJAXPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            loadChiTiet();
        }
        public void loadChiTiet()
        {
            var giohang = Session["listGioHang"] as List<SanPham>;
            if (giohang != null)
            {
                long longTongSoLuong = 0;
                long longTongTien = 0;
                foreach (var sanpham in giohang)
                {
                    longTongSoLuong += sanpham.SoLuongMua;
                    longTongTien += sanpham.TongSoTien;
                }
                lbTongSanPham.InnerText = longTongSoLuong.ToString();
                lbTongTien.InnerText = longTongTien.ToString();
            }
            else
            {
                lbTongSanPham.InnerText = "0";
                lbTongTien.InnerText = "0";
            }
            using (var userService = new SanPhamSrv.SanPhamServiceClient())
            {
                long idsanpham = (Request.QueryString["sanpham"] == "" ? 0 : Convert.ToInt64(Request.QueryString["sanpham"]));
                //long idsanpham = 1;
                if (idsanpham == 0) Response.Redirect("~/TrangChu.aspx");
                else
                {
                    var modelsanpham = userService.ChiTietSanPham(idsanpham);
                    if (modelsanpham != null)
                    {
                        hdfIdSanPham.Value = idsanpham.ToString();

                        lbTenSanPham.InnerText = modelsanpham.Ten;
                        lbTenSanPham1.InnerText = modelsanpham.Ten;
                        lbTenSanPham2.InnerText = modelsanpham.Ten;
                        lbTenSanPham3.InnerText = modelsanpham.Ten.ToUpper();
                        lbGiaTien.InnerText = modelsanpham.Gia.ToString();

                        lvNoiDungCT.DataSource = modelsanpham.lstNoiDung;
                        lvNoiDungCT.DataBind();
                        lvNoiDungCT1.DataSource = modelsanpham.lstNoiDung;
                        lvNoiDungCT1.DataBind();

                        lvAnhChiTiet.DataSource = modelsanpham.lstAnh;
                        lvAnhChiTiet.DataBind();

                        lvAnhChiTiet1.DataSource = modelsanpham.lstAnh;
                        lvAnhChiTiet1.DataBind();

                        if (modelsanpham.SoLuong > 0)
                        {
                            btMuaHang.Visible = true;
                            btHetHang.Visible = false;

                            btMuaHang1.Visible = true;
                            btHetHang1.Visible = false;
                        }
                        else
                        {
                            btMuaHang.Visible = false;
                            btHetHang.Visible = true;

                            btMuaHang1.Visible = false;
                            btHetHang1.Visible = true;
                        }
                        var lstloaisp = userService.DanhSachLoaiSanPham();
                        foreach (var lsp in lstloaisp)
                        {
                            if (lsp.Id == modelsanpham.IdLoai) lbLoaiSanPham.InnerText =lsp.Ten.ToUpper();
                        }
                    }
                }
            }
        }

        public string MuaHang(string strSanphamid)
        {
            string a = "Thất bại";

            using (var userService = new SanPhamSrv.SanPhamServiceClient())
            {
                var modelsanpham = userService.ChiTietSanPham(Convert.ToInt64(strSanphamid));
                if (modelsanpham != null)
                {
                    //modelsanpham.SoLuong = 1;
                    var listGioHang = Session["listGioHang"] as List<SanPham>;

                    //Check trung san pham trong list
                    bool check = false;
                    if (listGioHang == null) listGioHang = new List<SanPham>();
                    foreach (var sanpham in listGioHang)
                    {
                        
                        if (sanpham.Id.ToString() == strSanphamid) check = true;

                        
                    }
                    if (check == true)
                    {
                        a = "Sản phẩm đã có trong giỏ hàng";
                    }
                    else
                    {
                        modelsanpham.SoLuongMua = 1;
                        modelsanpham.TongSoTien = modelsanpham.Gia * modelsanpham.SoLuongMua;
                        listGioHang.Add(modelsanpham);
                        Session["listGioHang"] = listGioHang;
                        a = "Đã thêm sản phẩm vào giỏ hàng";
                    }

                    var giohang = Session["listGioHang"] as List<SanPham>;
                    if (giohang != null)
                    {
                        long longTongSoLuong = 0;
                        long longTongTien = 0;
                        foreach (var sanpham in giohang)
                        {
                            longTongSoLuong += sanpham.SoLuongMua;
                            longTongTien += sanpham.TongSoTien;
                        }
                        lbTongSanPham.InnerText = longTongSoLuong.ToString();
                        lbTongTien.InnerText = longTongTien.ToString();
                    }
                    UpdatePanel("cbpTongSanPham");
                }
            }
            return a;
        }


    }

}